package com.runner.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.runner.game.GameHandler;
import com.runner.observer.Event;
import com.runner.observer.Subject;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class DeathScreen implements Constants{

	private NinePatch patch;
	private BitmapFont font;
	private Stage stage;
	private GameHandler handler;
	private GameScreen gameScreen;
	private TextButton buttonBack, buttonPlay;
	private Skin skin;
	private TextureAtlas atlas;
	private TextButtonStyle playStyle;
	private boolean countingCoins;
	private int coins;
	private int distance;
	private int topDistance;
	private Subject subject;
	private float coinTimer;
	private float distanceTimer;
	private int bonus;
	private String title;
	private String bonusCoinString;
	private static final float COIN_TIME = 0.01F;
	private static final float DISTANCE_TIME = 0.003F;

	public DeathScreen(GameScreen gameScreen, GameHandler handler, BitmapFont smallFont, Subject subject) {
		patch = new NinePatch(Locator.getAssets().get(PANEL_PATH, Texture.class));
		font = smallFont;
		this.handler = handler;
		this.subject = subject;
		this.gameScreen = gameScreen;
		title="Results!";
		bonusCoinString="";
		bonus=0;
		topDistance=0;

		atlas = new TextureAtlas(Gdx.files.internal("data/button.pack"));
		skin = new Skin(atlas);
		playStyle = new TextButtonStyle();
		playStyle.up = skin.getDrawable("button.up");
		playStyle.down = skin.getDrawable("button.down");
		playStyle.pressedOffsetX = 1;
		playStyle.pressedOffsetY = -1;
		playStyle.font = smallFont;
		playStyle.fontColor = Locator.getPlayerTextColor(handler.getpType());
		font.setColor(Color.DARK_GRAY);

		stage = new Stage(Locator.getViewport());
		createButtons();
		buttonBack.setPosition(ACTUAL_VIRTUAL_WIDTH*0.3f, ACTUAL_VIRTUAL_HEIGHT*0.24f);
		buttonPlay.setPosition(ACTUAL_VIRTUAL_WIDTH*0.625f, ACTUAL_VIRTUAL_HEIGHT*0.24f);
		stage.addActor(buttonBack);
		stage.addActor(buttonPlay);
		
		
	}

	public void render(SpriteBatch sb, float delta) {
		String multi="";
		//draws is the player had a coin multiplier
		if(gameScreen.isCoinMultiplier()){
			multi=" (x2)";
		}
		sb.begin();
		patch.draw(sb, ACTUAL_VIRTUAL_WIDTH * 0.25f, ACTUAL_VIRTUAL_HEIGHT * 0.2f, ACTUAL_VIRTUAL_WIDTH * 0.5f,
				ACTUAL_VIRTUAL_HEIGHT * 0.6f);
		font.draw(sb, ""+title, ACTUAL_VIRTUAL_WIDTH * 0.45f, ACTUAL_VIRTUAL_HEIGHT * 0.75f);
		font.draw(sb, "Coins"+multi+bonusCoinString+" : " + coins, ACTUAL_VIRTUAL_WIDTH * 0.3f, ACTUAL_VIRTUAL_HEIGHT * 0.65f);
		font.draw(sb, "Top Distance: "+topDistance, ACTUAL_VIRTUAL_WIDTH * 0.3f, ACTUAL_VIRTUAL_HEIGHT * 0.53f);
		font.draw(sb, "Distance: " + distance, ACTUAL_VIRTUAL_WIDTH * 0.3f, ACTUAL_VIRTUAL_HEIGHT * 0.41f);
		sb.end();
		stage.act(delta);
		stage.draw();
		updateValues(delta);
		
		if (!gameScreen.getAudioManager().isPlayingGameMusic() && !gameScreen.getAudioManager().isSwitchingMusic()) {
			subject.notify(Event.EVENT_MUSIC_FINISHED);
		}
	}

	/**
	 * Tallies coins and distance for dramatic effect
	 * @param delta
	 */
	private void updateValues(float delta) {
		if (countingCoins) {
			coinTimer += delta;
			if (coinTimer > COIN_TIME) {
				if (coins < handler.getCoins()) {
					coins += 2;
				} else {
					countingCoins = false;
					coins=handler.getCoins();
				}
				coinTimer = 0;
			}
		} else {
			distanceTimer += delta;
			if (distanceTimer > DISTANCE_TIME) {
				if (distance < handler.getDistance()) {
					distance += 3;
				} else {
					distance=handler.getDistance();
				}
				if (topDistance < handler.getTopDistance()) {
					topDistance += 3;
				} else {
					topDistance = handler.getTopDistance();
				}
				distanceTimer = 0;
			}
		}
	}

	private void createButtons() {
		buttonPlay = new TextButton("Play", playStyle);
		buttonPlay.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(Event.EVENT_CLICK);
				hide();
				gameScreen.finishDeathScreen(true);
			}
		});

		buttonBack = new TextButton("Loadout", playStyle);
		buttonBack.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(Event.EVENT_CLICK);
				hide();
				gameScreen.finishDeathScreen(false);
			}
		});

	}

	public void hide() {
		Gdx.input.setInputProcessor(null);
	}

	/**
	 * called when player wins or dies
	 * @param death
	 */
	public void show(boolean death, boolean newTopDistance) {
		Gdx.input.setInputProcessor(stage);
		countingCoins = true;
		coins = 0;
		distance = 0;
		if(newTopDistance){
			topDistance=0;
		} else {
			topDistance=handler.getTopDistance();
		}
		coinTimer = 0;
		distanceTimer = 0;
		//if the player finished the level add 20% bonus coins and change title
		if(!death){
			title="Nice job!";
			bonusCoinString="+20%";
			bonus=(int)(handler.getCoins()*0.2f);
			handler.addCoins(bonus);
		} else {
			title="Results!";
			bonusCoinString="";
			bonus=0;
		}
	}

}
